//
//  Request.m
//  Cheirapsis
//
//  Created by wangning on 15/11/4.
//  Copyright © 2015年 mohe. All rights reserved.
//

#import "Request.h"
#import "AVOSCloud.h"
#import "iToast.h"
#import "MBProgressHUD.h"

@implementation Request
//alloc一次的全局可用，anyWhere都可以
+(Request*)sharedInstance{
    static dispatch_once_t onceToken;
    static Request* sharedInstance;
    dispatch_once(&onceToken, ^{
        sharedInstance = [[Request alloc]init];
    });
    return sharedInstance;
}
//广告页图片
- (void)getAdsPic:(NSMutableDictionary *)postParems content:(void(^)(NSArray *arr))requestResult{
    AVQuery *q = [AVQuery queryWithClassName:@"Ads"];
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    [q findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        [objects enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            AVObject *ob = (AVObject *)obj;
            AVFile *f = ob[@"image"];
            [arr addObject:f.url];
        }];
        requestResult(arr);
    }];
}
//注册接口
- (void)registers:(NSString *)userName pw:(NSString *)passWord nick:(NSString *)nickName{
    AVUser *user = [AVUser objectWithClassName:@"_User"];
    [user setObject:userName forKey:@"username"];
    [user setObject:passWord forKey:@"password"];
    [user setObject:nickName forKey:@"nickName"];
    [user setObject:@(1) forKey:@"Role"];
    [user setObject:@(0) forKey:@"isVip"];
    [user signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [[iToast makeText:@"注册成功"] show];
        }else if (error){
            [[iToast makeText:@"注册失败"] show];
        }
    }];
}
//店铺接口
- (void)getStoreName:(NSMutableDictionary *)postParems content:(void(^)(NSArray *arr))requestResult{
    AVQuery *store = [AVQuery queryWithClassName:@"Store"];
    NSMutableArray *arr  = [[NSMutableArray alloc] init];
    [store findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        [objects enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary *ob = (NSDictionary *)obj;
            [arr addObject:ob];
        }];
        requestResult(arr);
    }];
}
//语音帮助
- (void)getVoiceHelp:(NSMutableDictionary *)postParems content:(void(^)(NSArray *array))requset{
    AVQuery *voice = [AVQuery queryWithClassName:@"VoiceHelp"];
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    [voice findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        [objects enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            AVObject *ob = (AVObject *)obj;
            AVFile *file = ob[@"voiceFile"];
            
            [arr addObject:file.url];
        }];
        requset(arr);
    }];
}
//获取订单接口
- (void)getOrderList:(NSMutableDictionary *)postParems content:(void (^)(NSArray *arr))requset{
    NSMutableArray *arr = [[NSMutableArray alloc] init];
    AVQuery *order = [AVQuery queryWithClassName:@"OrderList"];
    if ([[NSUserDefaults standardUserDefaults] valueForKey:@"userName"] == nil) {
        [[iToast makeText:@"您还没有登陆"] show];
        return;
    }else{
    [order whereKey:@"username" equalTo:[[NSUserDefaults standardUserDefaults] valueForKey:@"userName"]];
    [order findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        [objects enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary *dic = (NSDictionary *)obj;
            [arr addObject:dic];
        }];
        requset(arr);
    }];
    }
}
//发起订单
- (void)issueOrder:(NSDictionary *)dic{
    AVUser *issue = [AVUser objectWithClassName:@"OrderList"];
    [issue setObject:dic[@"money"] forKey:@"money"];
    [issue setObject:dic[@"username"] forKey:@"username"];
    [issue setObject:@"未接单" forKey:@"orderStatus"];
    [issue setObject:dic[@"phone"] forKey:@"customerTel"];
    [issue setObject:dic[@"address"] forKey:@"storeAddress"];
    [issue setObject:dic[@"type"] forKey:@"anmoKind"];
    [issue setObject:dic[@"storeName"] forKey:@"StoreName"];
    [issue setObject:dic[@"time"] forKey:@"arriveTime"];
    [issue setObject:dic[@"storeTel"] forKey:@"storeTel"];
    [issue setObject:dic[@"contacts"] forKey:@"contacts"];
    [issue setObject:dic[@"renarks"] forKey:@"remarks"];
    [issue signUpInBackgroundWithBlock:^(BOOL succeeded, NSError *error) {
        if (succeeded) {
            [[iToast makeText:@"订单成功"] show];
        }else if (error){
            [[iToast makeText:@"订单失败"] show];
        }
    }];

}
//按摩服务接口
-(void)getAnmo:(NSMutableDictionary *)postParems content:(void(^)(NSArray *arr))requset{
    NSMutableArray * array = [[NSMutableArray alloc]init];
    AVQuery * anmo =[AVQuery queryWithClassName:@"anMoService"];
    [anmo findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
        [objects enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            NSDictionary * dic = (NSDictionary *)obj;
            [array addObject:dic];
        }];
        requset(array);
    } ];
}
//我的足迹
- (void)getMyFeet:(NSMutableDictionary *)postpostParems content:(void(^)(NSArray *arr))requset{
    AVUser *u = [AVUser currentUser];
    NSArray *storeName = u[@"myFeet"];
    
    AVQuery *myfeet = [AVQuery queryWithClassName:@"Store"];
    NSMutableArray *storeArray = [[NSMutableArray alloc] init];
    for (int i = 0; i < storeName.count; i++) {
        [myfeet whereKey:@"storeName"  equalTo:storeName[i]];
        [myfeet findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
            [objects enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                NSDictionary *dic = (NSDictionary *)obj;
                [storeArray addObject:dic];
            }];
            requset(storeArray);
        }];
        
    }
}

//个人设置
-(void)shezhi:(NSMutableDictionary *)postParems content:(void(^)(NSArray *arr))requset{
    AVUser *use = [AVUser currentUser];

    
    AVQuery * q =[AVQuery queryWithClassName:@"_User"];
    
    [q whereKey:@"username" equalTo:use[@"username"]];
    

}


@end
